package org.teamh.logic.datamodel;

import org.teamh.game.Start;

public class Tower {

	private boolean building;
	private double buildProgress;
	private boolean upgrading;
	private boolean selling;
	private String stratetgy = "CLOSEST";
	private String strategyForSimulation;
	private long lastShot;
	private long lastShotForSimulation;
	
	private UserAssets userAssets;
	private Cell cell;
	private Creep firesOn;
	private String ID;
	private String type;
	private int level;
	private int levelForSimulation;
	private String color;
	
	private TowerDescription towerDesciDescription;
	private TowerDescription towerDescriptionForSimulation;
	private double cooldownForSimulation;
	
	private Start start;
	
	private boolean changedBySimulation;
	
	public Tower(String ID, Start start) {
		this.ID = ID;
		this.start = start;
	}
	
	public Cell getCell() {
		return cell;
	}

	public void setCell(Cell cell) {
		if(this.cell != cell) {
			if(getCell() != null) {
				Cell oldCell = getCell();
				this.cell = null;
				oldCell.setTower(null);
			}
			this.cell = cell;
			if(getCell() != null) {
				getCell().setTower(this);
			}
		}
	}
	
	public void setUserAssets(UserAssets userAssets) {
		if(this.userAssets != userAssets) {
			if(getUserAssets() != null) {
				getUserAssets().removeTower(this);
			}
			this.userAssets= userAssets;
			if(getUserAssets() != null) {
				getUserAssets().addTower(this);
			}
		}
	}
	
	public UserAssets getUserAssets() {
		return userAssets;
	}

	public boolean isBuilding() {
		return building;
	}

	public void setBuilding(boolean building) {
		this.building = building;
	}

	public double getBuildProgress() {
		return buildProgress;
	}

	public void setBuildProgress(double buildProgress) {
		this.buildProgress = buildProgress;
	}

	public boolean isUpgrading() {
		return upgrading;
	}

	public void setUpgrading(boolean upgrading) {
		this.upgrading = upgrading;
	}

	public boolean isSelling() {
		return selling;
	}

	public void setSelling(boolean selling) {
		this.selling = selling;
	}

	public String getStratetgy() {
		return stratetgy;
	}

	public void setStratetgy(String stratetgy) {
		this.stratetgy = stratetgy;
	}
	
	public void setFiresOn(Creep firesOn) {
		if(this.firesOn != firesOn) {
			if(getFiresOn() != null) {
				Creep oldFiresOn = getFiresOn();
				this.firesOn = null;
				oldFiresOn.removeFiredOn(this);
			}
			this.firesOn = firesOn;
			if(getFiresOn() != null) {
				getFiresOn().addFiredOn(this);
			}
		}
	}
	
	public Creep getFiresOn() {
		return firesOn;
	}
	
	public long getLastShot() {
		return lastShot;
	}
	
	public void setLastShot(long lastShot) {
		this.lastShot = lastShot;
	}
	
	public boolean canFire() {
		return Math.abs(lastShot - System.currentTimeMillis()) >= towerDesciDescription.getCoolDownMax();
	}
	
	public String getID() {
		return ID;
	}
	
	public String getType() {
		return type;
	}
	
	public void setType(String type) {
		this.type = type;
		selectTowerDesctiption();
	}
	
	public int getLevel() {
		return level;
	}
	
	public void setLevel(int level) {
		this.level = level;
		selectTowerDesctiption();
	}
	
	private void selectTowerDesctiption() {
		if(type != null && level != 0) {
			for(TowerDescription t: start.getLogic().getManager().getTowerDescriptions().values()) {
				if(t.getType().equals(type) && t.getLevel() == level) {
					this.towerDesciDescription = t;
					break;
				}
			}
		}
	}
	
	public String getColor() {
		return color;
	}
	
	public void setColor(String color) {
		this.color = color;
	}

	public TowerDescription getTowerDescription() {
		return towerDesciDescription;
	}

	public void setTowerDesciDescription(TowerDescription towerDesciDescription) {
		this.towerDesciDescription = towerDesciDescription;
	}

	/*************************************************************************
	 * getter and setter for simulation
	 ************************************************************************/
	
	public void setStrategyForSimulation(String strategyForSimulation) {
		this.strategyForSimulation = strategyForSimulation;
	}

	public String getStrategyForSimulation() {
		return strategyForSimulation;
	}

	public void setLastShotForSimulation(long lastShotForSimulation) {
		this.lastShotForSimulation = lastShotForSimulation;
	}

	public long getLastShotForSimulation() {
		return lastShotForSimulation;
	}

	public void setLevelForSimulation(int levelForSimulation) {
		this.levelForSimulation = levelForSimulation;
		selectTowerDesctiptionForSimulation();
	}

	private void selectTowerDesctiptionForSimulation() {
		if(type != null && levelForSimulation != 0) {
			for(TowerDescription t: start.getLogic().getManager().getTowerDescriptions().values()) {
				if(t.getType().equals(type) && t.getLevel() == levelForSimulation) {
					this.towerDesciDescription = t;
					break;
				}
			}
		}
	}

	public int getLevelForSimulation() {
		return levelForSimulation;
	}

	public void setTowerDescriptionForSimulation(
			TowerDescription towerDescriptionForSimulation) {
		this.towerDescriptionForSimulation = towerDescriptionForSimulation;
	}

	public TowerDescription getTowerDescriptionForSimulation() {
		return towerDescriptionForSimulation;
	}
	
	public boolean canFireForSimulation(long timeStamp) {
		return Math.abs(lastShotForSimulation - timeStamp) >= cooldownForSimulation;
	}
	
	public void setChangedBySimulation(boolean changedBySimulation) {
		this.changedBySimulation = changedBySimulation;
	}
	
	public boolean isChangedBySimulation() {
		return changedBySimulation;
	}
	
	public void setCooldownForSimulation(double cooldownForSimulation) {
		this.cooldownForSimulation = cooldownForSimulation;
	}
	
	public double getCooldownForSimulation() {
		return cooldownForSimulation;
	}
}

